#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct
{
    int data[MAXSIZE];
    int top;
}SqStack;

void show_Stack(SqStack *S);
// 初始化栈  
void InitStack(SqStack *S) ;
// 判断栈空  
int StackEmpty(SqStack *S);
//判断栈满
int StackFull(SqStack *S);
//入栈
int push_stack(SqStack *S,int value);
//出栈
int pop_stack(SqStack *S,int *value);
//获取栈顶元素
int getTopdata(SqStack *S,int *value);

int main(int argc, char const *argv[])
{
    SqStack stack;
    InitStack(&stack);
    if(!push_stack(&stack,1)) ; else printf("stack is full\n");
    if(!push_stack(&stack,12)) ; else printf("stack is full\n");
    if(!push_stack(&stack,13)) ; else printf("stack is full\n");
    if(!push_stack(&stack,5)) ; else printf("stack is full\n");
    if(!push_stack(&stack,6)) ; else printf("stack is full\n");
    if(!push_stack(&stack,7)) ; else printf("stack is full\n");
    if(!push_stack(&stack,2)) ; else printf("stack is full\n");
    if(!push_stack(&stack,3)) ; else printf("stack is full\n");
    if(!push_stack(&stack,4)) ; else printf("stack is full\n");
    int dat = 0;
    show_Stack(&stack);

    if(!getTopdata(&stack,&dat))
    {
        printf("栈顶元素：%d\n",dat);
    }
    //出栈
    if( !pop_stack(&stack,&dat))
    show_Stack(&stack);
    printf("出栈数据为：%d\n",dat);
    if( !pop_stack(&stack,&dat))
    show_Stack(&stack);
    printf("出栈数据为：%d\n",dat);
    if( !pop_stack(&stack,&dat))
    show_Stack(&stack);
    printf("出栈数据为：%d\n",dat);

    return 0;
}


// 初始化栈  
void InitStack(SqStack *S)
{
    S->top = -1;
}
// 判断栈空  
int StackEmpty(SqStack *S)
{
    if (S->top == -1)
    {
        return 1;
    }
    return 0;   
}
//判断栈满
int StackFull(SqStack *S)
 {  
    return S->top == MAXSIZE-1;  
} 

void show_Stack(SqStack *S)
{
    for (int i = S->top; i >= 0; i--)
    {
        printf("%d ",S->data[i]);
    }
    printf("\n");
    
}


//入栈
int push_stack(SqStack *S,int value)
{
    if(StackFull(S))
    {
        printf("STACK IS FULL\n");
        return 1;
    }
    //入栈  数据放在栈顶
    S->data[++S->top]=value;
    return 0;
}

//出栈
int pop_stack(SqStack *S,int *value)
{
   
    //判空
    if(StackEmpty(S))
    {  printf("stack is empty\n");
        return 1;
    }
    *value=S->data[S->top--];
   
    return 0;
}

//获取栈顶元素
int getTopdata(SqStack *S,int *value)
{
    //判空
    if(StackEmpty(S))
    {  printf("stack is empty\n");
        return 1;
    }
    *value=S->data[S->top];
    return 0;
}